﻿using System;
using System.IO;
using System.Reflection;
using System.Data;
using System.Collections.Generic;
using System.Xml.Serialization;

namespace Freedom.Common
{
    /// <summary>
    /// TypeHelper
    /// </summary>
    public static class TypeHelper
    {
        /// <summary>
        /// Gets the type reference.
        /// </summary>
        /// <param name="type">The type.</param>
        /// <returns></returns>
        public static string GetTypeReference(this Type type)
        {
            return string.Format("{0}, {1}", type.FullName, type.Assembly.GetName().Name);
        }
        /// <summary>
        /// Gets the attribute.
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="type">The type.</param>
        /// <param name="inherit">if set to <c>true</c> [inherit].</param>
        /// <returns></returns>
        public static T GetAttribute<T>(this ICustomAttributeProvider type, bool inherit)
            where T : class
        {
            var attr = type.GetCustomAttributes(typeof(T), inherit);
            return (attr.Length == 0) ? null : (T)attr[0];
        }
        /// <summary>
        /// Gets from XML.
        /// </summary>
        /// <param name="type">The type.</param>
        /// <param name="xml">The XML.</param>
        /// <returns></returns>
        public static object GetFromXml(this Type type, string xml)
        {
            if (string.IsNullOrEmpty(xml)) return null;

            var serizalizer = new XmlSerializer(type);
            using (var reader = new StringReader(xml))
            {
                return serizalizer.Deserialize(reader);
            }
        }
        /// <summary>
        /// Gets from XML.
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="xml">The XML.</param>
        /// <returns></returns>
        public static T GetFromXml<T>(string xml)
        {
            if (string.IsNullOrEmpty(xml)) return default(T);

            var serizalizer = new XmlSerializer(typeof(T));
            using (var reader = new StringReader(xml))
            {
                return (T)serizalizer.Deserialize(reader);
            }
        }

        /// <summary>
        /// convert type list to datatable
        /// </summary>
        /// <typeparam name="T">type</typeparam>
        /// <param name="entitys">object list</param>
        /// <returns></returns>
        public static DataTable ListToDataTable<T>(IList<T> entitys)
        {
            var result = new DataTable();

            //empty check
            if (entitys == null || entitys.Count < 1)
                return result;

            //get the type and properties
            var entityType = entitys[0].GetType();
            var entityProperties = entityType.GetProperties();

            //add column
            foreach (var t in entityProperties)
            {
                result.Columns.Add(t.Name);
            }

            //add the row
            foreach (object entity in entitys)
            {
                var entityValues = new object[entityProperties.Length];
                for (int i = 0; i < entityProperties.Length; i++)
                {
                    entityValues[i] = entityProperties[i].GetValue(entity, null);

                }
                result.Rows.Add(entityValues);
            }
            return result;
        }
    }
}
